﻿var username;
var secretkey;
var staff_id;
(function($){
	$(window).load(function(){
		username = $.session.get("username");
		secretkey = $.session.get("user_secretkey");
		staff_id = $.session.get("staff_id");
		find_staff();	//员工
		find_staff_managementById();
		
	})
})(jQuery);

function find_staff_managementById(){
	$.ajax({
		url: "../../../../service",
		//async:false,
		//cache:false,
		data : {
			proc : "g_xmy.find_staff_managementById",
			para : "['" + username + "','" + secretkey + "','" + staff_id + "']"
		},
		dataType : "json",
		success : function(data) {
			console.log(JSON.stringify(data));
			if (data[0].TAG == "0") {
				window.location.href = "./error.html";
			} else {
				document.getElementById("login_name").value = data[0].NAME;
				document.getElementById("staff_name").value = data[0].NICKNAME;			
				document.getElementById("gw_style").value = data[0].TYPE;
				$("#shop_id").find("option[value='"+data[0].WORK_ID+"']").attr("selected",true); 
				/*var a = $("#vips").find("input");				
				var storeids="";
			
				for(var b=0;b<=a.length-1;b++){
					var s=$(a[b]).val();
				//	console.log(s+"===s");
					for(var ss=0;ss<data.length;ss++){
						if(data[ss].WORK_ID==s){
							$(a[b]).attr("checked","checked");
						}
					}
					
				}*/
				
				
			}
		}
	});
}
//工位全选
function check1(){
	var check = document.getElementById("vip");
	var s = document.getElementById("vips");
	var n = s.getElementsByTagName("input");
	for(i = 0;i < n.length;i++){//循环数组
		if (check.checked == false) {
			n[i].checked = false;
		} else{
			n[i].checked = true;
		}
	}
}
//加盟店全选
function check2(){
	var check = document.getElementById("vip2");
	var s = document.getElementById("vips_2");
	var n = s.getElementsByTagName("input");
	for(i = 0;i < n.length;i++){//循环数组
		if (check.checked == false) {
			n[i].checked = false;
		} else{
			n[i].checked = true;
		}
	}
}
//获取所有工位
function find_staff(){
	username = $.session.get("username");
	secretkey = $.session.get("user_secretkey");
	$("#shop_id").html("");
		$.ajax({
			url: "../../../../service",
			data: {
				proc: "g_xmy.find_stationAll",
				para: "['" + username + "','" + secretkey + "']"
			},
			dataType: "json",
			success: function(data) {
				listCount = data.length;
				/*var len = $("#vips").length;
				if(len>0){
					$("#vips").empty();
				}*/
				if(data[0].TAG == "0"){
					window.location.href="./error.html";
				}else{
					var options="<option value='0'>请选择</option>";
					for(var a=0;a<data.length;a++){
						/*var al = document.getElementById("vips");
						var div =document.createElement("li");
						div.innerHTML=	'<label class="check-box" style="float:left;margin-left:20px;">
						<input type="checkbox" name="hdf" value="'+data[a].ID+'"/><span>'+data[a].NAME+'</span></label>'
						al.appendChild(div);*/
						options+='<option value="'+data[a].ID+'">'+data[a].NAME+'</option>';
					}
					$("#shop_id").html(options);
				}
			}
		});
}
//获取所有工位
/*function find_staff(){
	username = $.session.get("username");
	secretkey = $.session.get("user_secretkey");
		$.ajax({
			url: "../../../../service",
			data: {
				proc: "g_xmy.find_stationAll",
				para: "['" + username + "','" + secretkey + "']"
			},
			dataType: "json",
			success: function(data) {
				listCount = data.length;
				var len = $("#vips").length;
				if(len>0){
					$("#vips").empty();
				}
				if(data[0].TAG == "0"){
					window.location.href="./error.html";
				}else{
					for(var a=0;a<data.length;a++){
						var al = document.getElementById("vips");
						var div =document.createElement("li");
						div.innerHTML=	'<label class="check-box" style="float:left;margin-left:20px;"><input type="checkbox" name="hdf" value="'+data[a].ID+'"/><span>'+data[a].NAME+'</span></label>'
						al.appendChild(div);
					}
				}
			}
		});
}
*/

//工号是否合法
function there_exist(){
	var login_name = document.getElementById('login_name').value;
/*	if (!(/^[0-9A-Za-z_]{5,12}$/.test(login_name))) {
		alert("登录名名格式有误，提示：5-12位字母或字母加数字。");
		return false;
	}	*/
	if (!(/^[0-9]{4,11}$/.test(login_name))) {
		layer.alert("工号名格式有误，提示：4-11位数字。");
		return false;
	}
	$.ajax({
		url: "../../../../service",
		data: {
			proc: "g_xmy.there_exist",
			para: "['" + login_name + "']"
		},
		dataType: "json",
		success: function(data) {
			if(data[0].TAG == "0"){
				document.getElementById("is_verification").innerHTML='<i style="color:red;">账号已存在，请重新输入！</i>';
			}else if(data[0].TAG == "1"){
				document.getElementById("is_verification").innerHTML='<i style="color:green;">账号可以使用！</i>';
			}else{
				layer.alert("请填写登录名");
			}
		}
	});
}
//修改员工
function up_staff_management(){ 
	var login_name = document.getElementById('login_name').value;
	var staff_name = document.getElementById('staff_name').value;

	/*var ac="";
	var a = document.getElementsByTagName("input"); 
	for(var b=0;b<a.length;b++){ 
		if(a[b].getAttribute("type")=="checkbox"){
			if(a[b].checked==true){ 
				ac += a[b].value+",";
			}
		}
	}
	var jurisdiction = ac.substring(0,ac.length-1);*/
	var jurisdiction=$("#shop_id option:selected").val(); //工位//工位
	
//	alert("工位=="+jurisdiction);
	
	if (staff_name == '') {
		layer.alert("请输入姓名！");
		return false;
		//如果使用return，js的代码将继续执行。这是为什么？
	}
	if (!checkChinese(staff_name)) {
		layer.alert("请输入汉字，并且长度为2-8位");
		return false;
	}
	
	if(jurisdiction == '' && jurisdiction.length<=0){
		layer.alert("请为员工选择工位");
		return false;
	}
	$.ajax({
		url: "../../../../service",
		cache : false,
		data : {
			proc : "g_xmy.up_staff_management",
			para : "['" + username + "','" + secretkey + "','" + login_name + "','" + staff_name  + "','" + jurisdiction + "','" + staff_id + "']"
		},
		dataType : "json",
		success : function(data) {
			if (data[0].TAG == "1") {
				layer.alert("员工修改成功。",function(index){
					window.location.href="./factory_list.html";
					layer.close(index);
				});
			} else {
				layer.alert("员工修改失败！");
			}
		}
	});
}



//校验只能输入汉字，并且长度为2-4
function checkChinese(name) {
	reg = /^[\u4E00-\u9FA5]{2,8}$/;

    if (reg.test(name)) {
		return true;
    } else {
        return false;
    }
 }
 
 function re(){
	 window.location.href='./factory_list.html';
 }
 
 
 